package com.example.demo.utils;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.Poi;

import java.util.List;

/**
 * @auther: Wyz
 * @date: 2021/12/3 21:40
 * @return
 */
public class GaodeAddress {

    public static final String GD_KEY="679f779c65cc5528b6e48d389a30d8ed";

    /**
     * 高德地图通过文本关键字搜索地点信息
     * @param address:
     * @return void
     * @date 2021/12/4 13:53
     * @author wyz
    */
    public static List<Poi> queryLatitudeAndLongitude(String address){
        String url = "https://restapi.amap.com/v5/place/text?key="+GD_KEY+"&keywords="+address;
        String body = HttpRequest.get(url)
                .execute().body();
        JSONObject jObj = (JSONObject) JSON.parse(body);
        if (jObj.getString("info").equalsIgnoreCase("OK")) {
            JSONArray pois = jObj.getJSONArray("pois");
            List<Poi> pois1 = JSON.parseArray(pois.toString(), Poi.class);
            return pois1;
        }
        return null;
    }

    public static void main(String[] args){
        List<Poi> 滩邱 = queryLatitudeAndLongitude("武邱乡滩邱村");
        System.err.println(滩邱);
    }
}
